<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/img/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="quit">退出</el-button></el-header
    >
    <el-container>
      <!-- 左侧区域 -->
      <el-aside :width="asideWidth + 'px'">
        <div class="taggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#323743"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isTaggle"
          :collapse-transition="false"
          router
          :default-active="isDefaultActive"
        >
          <el-submenu
            :key="index"
            v-for="(item, index) in menuList"
            :index="index + ''"
          >
            <template slot="title">
              <i :class="iconList[index]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item
              :key="subindex"
              v-for="(subitem, subindex) in item.children"
              :index="'/' + subitem.path"
            >
              <i class="el-icon-menu"></i>
              <span>{{ subitem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 引入子组件
// 引入路由请求
import { getMenuList } from "../network/home.js";
export default {
  name: "Hmoe",
  data() {
    return {
      menuList: [],
      iconList: [
        "iconfont icon-users",
        "iconfont icon-tijikongjian",
        "iconfont icon-shangpin",
        "iconfont icon-danju",
        "iconfont icon-baobiao",
      ],
      isTaggle: false,
    };
  },
  created() {
    this.getMenuList();
  },
  components: {
    // Welcome
  },
  methods: {
    // 普通方法
    // 退出登录
    quit() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    // 折叠菜单栏
    toggleCollapse() {
      this.isTaggle = !this.isTaggle;
    },

    // 网络请求方法
    // 获取菜单
    async getMenuList() {
      const res = await getMenuList();
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      console.log(res);
    },
  },
  computed: {
    asideWidth() {
      return this.isTaggle ? 64 : 200;
    },
    isDefaultActive() {
      return this.$route.path;
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: #373d3f;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  color: #fff;
  padding-left: 0;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  margin-left: 15px;
}
.taggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-aside {
  background-color: #323743;
}
.el-aside .el-menu {
  border-right: 0;
}
.el-main {
  background-color: #e9edf0;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 8px;
}
</style>
